using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
    [Serializable()]
    public partial class CVideo : CBaseDynamic, IComparable<CVideo>
    {
        #region Constructors
        //Public (Copy Constructor)
        public CVideo(CVideo original, CDataSrc target)
        {
            //Database Connection
            m_dataSrc = target;

            //Copy fields
            _videoFileName = original.VideoFileName;
            _videoFileSize = original.VideoFileSize;
            _videoDescription = original.VideoDescription;
            _videoBlob = original.VideoBlob;
            _videoThumb = original.VideoThumb;
            _videoMD5 = original.VideoMD5;
            _videoFBId = original.VideoFBId;
            _videoCreated = original.VideoCreated;
        }

        //Protected (Datareader/Dataset)
        protected CVideo(CDataSrc dataSrc, IDataReader dr) : base(dataSrc, dr) {}
        protected CVideo(CDataSrc dataSrc, DataRow     dr) : base(dataSrc, dr) {}
        #endregion

        #region Default Values
        protected override void InitValues_Auto()
        {
            //Null-Equivalent values (except String.Empty, as nulls tend to be inconvenient for string types)
            _videoId = int.MinValue;
            _videoFileName = string.Empty;
            _videoFileSize = int.MinValue;
            _videoDescription = string.Empty;
            _videoMD5 = Guid.Empty;
            _videoCreated = DateTime.MinValue;
            _videoBlob = new byte[]{};
            _videoFBId = long.MinValue;
            _videoThumb = new byte[]{};
        }
        #endregion

        #region Members
        protected int _videoId;
        protected string _videoFileName;
        protected int _videoFileSize;
        protected string _videoDescription;
        protected byte[] _videoBlob;
        protected byte[] _videoThumb;
        protected Guid _videoMD5;
        protected long _videoFBId;
        protected DateTime _videoCreated;
        #endregion

        #region Properties - Column Values
        //Primary Key Column (ReadOnly)
        public int VideoId    {   get { return _videoId;   } }

        //Table Columns (Read/Write)
        public string VideoFileName    {   get { return _videoFileName; } set { _videoFileName = value; }    }
        public int VideoFileSize    {   get { return _videoFileSize; } set { _videoFileSize = value; }    }
        public string VideoDescription    {   get { return _videoDescription; } set { _videoDescription = value; }    }
        public Guid VideoMD5    {   get { return _videoMD5; } set { _videoMD5 = value; }    }
        public DateTime VideoCreated    {   get { return _videoCreated; } set { _videoCreated = value; }    }
        public byte[] VideoBlob    {   get { return _videoBlob; } set { _videoBlob = value; }    }
        public long VideoFBId    {   get { return _videoFBId; } set { _videoFBId = value; }    }
        public byte[] VideoThumb    {   get { return _videoThumb; } set { _videoThumb = value; }    }

        //View Columns (ReadOnly)

        #endregion

        #region MustOverride Methods
        //Schema Information
        public const string TABLE_NAME      = "Videos";
        public const string VIEW_NAME       = "vwVideosLite";         //Used to override this.ViewName { get }
        public const string ORDER_BY_COLS   = "VideoFileName";   //See the CompareTo method below (Sql-based sorting should match In-Memory sorting)
        public const string SORTING_COLUMN  = "";
        public override string TableName {    get { return TABLE_NAME; } }
        protected override string ViewName {    get {   return VIEW_NAME;    }    }
        protected override string OrderByColumns {    get { return ORDER_BY_COLS; } }

        //CompareTo Interface (Default Sort Order)
        public int CompareTo(CVideo other) {   return this.VideoFileName.CompareTo(other.VideoFileName) ;  }

        //Primary Key Information
        public const string PRIMARY_KEY_NAME = "VideoId";
        protected override bool InsertPrimaryKey {  get { return false;    }   }
        protected override string PrimaryKeyName {  get { return PRIMARY_KEY_NAME;    }    }
        protected override object PrimaryKeyValue 
        {
              get { return _videoId; }
              set { _videoId = (int)value; }
        }

        //Factory Methods - Object
        protected override CBase MakeFrom(DataRow row)      {   return new CVideo(this.DataSrc, row);  }
        protected override CBase MakeFrom(IDataReader dr)   {   return new CVideo(this.DataSrc, dr);   }

        //Factory Methods - List
        protected override IList MakeList()                 {   return new CVideoList();               }
        protected override IList MakeList(int capacity)     {   return new CVideoList(capacity);       }

        //Convert from ADO to .Net
        protected override void ReadColumns(IDataReader dr)
        {
            _videoId = CAdoData.GetInt(dr, "VideoId");
            _videoFileName = CAdoData.GetStr(dr, "VideoFileName");
            _videoFileSize = CAdoData.GetInt(dr, "VideoFileSize");
            _videoDescription = CAdoData.GetStr(dr, "VideoDescription");
            _videoBlob = CAdoData.GetBytes(dr, "VideoBlob");
            _videoThumb = CAdoData.GetBytes(dr, "VideoThumb");
            _videoMD5 = CAdoData.GetGuid(dr, "VideoMD5");
            _videoFBId = CAdoData.GetLong(dr, "VideoFBId");
            _videoCreated = CAdoData.GetDate(dr, "VideoCreated");
        }
        protected override void ReadColumns(DataRow dr)
        {
            _videoId = CAdoData.GetInt(dr, "VideoId");
            _videoFileName = CAdoData.GetStr(dr, "VideoFileName");
            _videoFileSize = CAdoData.GetInt(dr, "VideoFileSize");
            _videoDescription = CAdoData.GetStr(dr, "VideoDescription");
            _videoBlob = CAdoData.GetBytes(dr, "VideoBlob");
            _videoThumb = CAdoData.GetBytes(dr, "VideoThumb");
            _videoMD5 = CAdoData.GetGuid(dr, "VideoMD5");
            _videoFBId = CAdoData.GetLong(dr, "VideoFBId");
            _videoCreated = CAdoData.GetDate(dr, "VideoCreated");
        }

        //Parameters for Insert/Update    
        protected override CNameValueList ColumnNameValues()
        {
            CNameValueList data = new CNameValueList();
            data.Add("VideoId", CAdoData.NullVal(_videoId));
            data.Add("VideoFileName", CAdoData.NullVal(_videoFileName));
            data.Add("VideoFileSize", CAdoData.NullVal(_videoFileSize));
            data.Add("VideoDescription", CAdoData.NullVal(_videoDescription));
            data.Add("VideoMD5", CAdoData.NullVal(_videoMD5));
            data.Add("VideoCreated", CAdoData.NullVal(_videoCreated));
            data.Add("VideoBlob", CAdoData.NullVal(_videoBlob));
            data.Add("VideoFBId", CAdoData.NullVal(_videoFBId));
            data.Add("VideoThumb", CAdoData.NullVal(_videoThumb));
            return data;
        }
        #endregion

        #region Queries - SelectAll/SelectWhere (inherited methods, cast only)
        //Normally used to load the cache
        public    new CVideoList SelectAll()                                                                           {   return (CVideoList)base.SelectAll();        }
        public    new CVideoList SelectAll(string orderBy)                                                             {   return (CVideoList)base.SelectAll(orderBy); }

        //Sometimes use a custom query to load the cache
        protected new CVideoList SelectWhere(string colName, ESign sign, object colValue)                              {   return (CVideoList)base.SelectWhere(colName, sign, colValue);                  }
        protected new CVideoList SelectWhere(CCriteria where)                                                          {   return (CVideoList)base.SelectWhere(where);                                    }
        protected new CVideoList SelectWhere(CCriteriaList where)                                                      {   return (CVideoList)base.SelectWhere(where);                                    }
        protected new CVideoList SelectWhere(CCriteriaList where, string tableOrJoin)                                  {   return (CVideoList)base.SelectWhere(where, tableOrJoin, this.OrderByColumns);  }
        protected new CVideoList SelectWhere(CCriteriaList where, string tableOrJoin, string orderBy)                  {   return (CVideoList)base.SelectWhere(where, tableOrJoin, orderBy);              }
        [Obsolete("Arbitrary where-clause is not necessary portable or safe from sql injection attacks. Consider using the parameterised query interfaces (or a stored proc)")]
        protected new CVideoList SelectWhere(string unsafeWhereClause)                                                 {   return (CVideoList)base.SelectWhere(unsafeWhereClause);                        }
        protected     CVideoList SelectById(int videoId)                                               {   return (CVideoList)base.SelectById(videoId);                    }
        protected     CVideoList SelectByIds(List<int> ids)                                         {   return (CVideoList)base.SelectByIds(ids);                                      }

        //Select Queries - Paged
        protected new CVideoList SelectAll(  CPagingInfo pi)                                               {    return (CVideoList)base.SelectAll(  pi);                          }
        protected new CVideoList SelectWhere(CPagingInfo pi, string name, ESign sign, object value)        {    return (CVideoList)base.SelectWhere(pi, name, sign, value);       }
        protected new CVideoList SelectWhere(CPagingInfo pi, CCriteria criteria)                           {    return (CVideoList)base.SelectWhere(pi, criteria);                }
        protected new CVideoList SelectWhere(CPagingInfo pi, CCriteriaList criteria)                       {    return (CVideoList)base.SelectWhere(pi, criteria);                }
        protected new CVideoList SelectWhere(CPagingInfo pi, CCriteriaList criteria, string viewOrJoin)    {    return (CVideoList)base.SelectWhere(pi, criteria, viewOrJoin);    }

        //Select Queries - Transactional (Internal scope for use in cascade deletes)
        internal new CVideoList SelectAll(                                                                                           IDbTransaction tx)  {    return (CVideoList)base.SelectAll(                                                 tx);    }
        internal new CVideoList SelectAll(string orderBy,                                                                            IDbTransaction tx)  {    return (CVideoList)base.SelectAll(orderBy,                                         tx);    }
        internal new CVideoList SelectWhere(string columnName,               object columnValue,                                     IDbTransaction tx)  {    return (CVideoList)base.SelectWhere(columnName,              columnValue,          tx);    }
        internal new CVideoList SelectWhere(string columnName,   ESign sign, object columnValue,                                     IDbTransaction tx)  {    return (CVideoList)base.SelectWhere(columnName,    sign,     columnValue,          tx);    }
        internal new CVideoList SelectWhere(                             CCriteria     criteria,                                     IDbTransaction tx)  {    return (CVideoList)base.SelectWhere(               criteria,                       tx);    }
        internal new CVideoList SelectWhere(                             CCriteriaList criteria,                                     IDbTransaction tx)  {    return (CVideoList)base.SelectWhere(               criteria,                       tx);    }
        internal new CVideoList SelectWhere(                             CCriteriaList criteria, string tableOrJoin,                 IDbTransaction tx)  {    return (CVideoList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        internal new CVideoList SelectWhere(                             CCriteriaList criteria, string tableOrJoin, string orderBy, IDbTransaction tx)  {    return (CVideoList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        internal     CVideoList SelectById(int videoId,                                                              IDbTransaction tx)  {    return (CVideoList)base.SelectById(videoId,                         tx);    }
        internal     CVideoList SelectByIds(List<int> ids,                                                        IDbTransaction tx)  {    return (CVideoList)base.SelectByIds(ids,                                           tx);    }

        //Select Queries - Stored Procs
        protected new CVideoList MakeList(string storedProcName,                           IDbTransaction txOrNull)    {   return (CVideoList)base.MakeList(storedProcName,             txOrNull);  }
        protected new CVideoList MakeList(string storedProcName, object[] parameters,      IDbTransaction txOrNull)    {   return (CVideoList)base.MakeList(storedProcName, parameters, txOrNull);  }
        protected new CVideoList MakeList(string storedProcName, CNameValueList parameters,IDbTransaction txOrNull)    {   return (CVideoList)base.MakeList(storedProcName, parameters, txOrNull);  }
        protected new CVideoList MakeList(string storedProcName, List<object> parameters,  IDbTransaction txOrNull)    {   return (CVideoList)base.MakeList(storedProcName, parameters, txOrNull);  }
        protected new CVideoList MakeList(string storedProcName, int param1,               IDbTransaction txOrNull)    {   return (CVideoList)base.MakeList(storedProcName, param1,     txOrNull);  }
        protected new CVideoList MakeList(string storedProcName, string param1,            IDbTransaction txOrNull)    {   return (CVideoList)base.MakeList(storedProcName, param1,     txOrNull);  }

        //Query Results
        protected new CVideoList MakeList(DataSet              ds) { return (CVideoList)base.MakeList(ds);        }
        protected new CVideoList MakeList(DataTable            dt) { return (CVideoList)base.MakeList(dt);        }
        protected new CVideoList MakeList(DataRowCollection  rows) { return (CVideoList)base.MakeList(rows);      }
        protected new CVideoList MakeList(IDataReader          dr) { return (CVideoList)base.MakeList(dr);        }
        protected new CVideoList MakeList(object           drOrDs) { return (CVideoList)base.MakeList(drOrDs);    }
        protected new CVideoList MakeList(byte[]             gzip) { return (CVideoList)base.MakeList(gzip);      }
        #endregion

        #region Queries - SelectBy[FK] (user-nominated fk/bool columns)
        //Note: These methods should only be used for cascade-deletes, or initialising the cache. Use CVideo.Cache.GetBy... for reqular queries

        //Non-Paged
        protected CVideoList SelectByMD5(Guid videoMD5)    {    return SelectWhere(new CCriteriaList("VideoMD5", videoMD5));    }
        protected CVideoList SelectByFBId(long videoFBId)    {    return SelectWhere(new CCriteriaList("VideoFBId", videoFBId));    }

        //Paged
        protected CVideoList SelectByMD5(CPagingInfo pi, Guid videoMD5)    {    return SelectWhere(pi, new CCriteriaList("VideoMD5", videoMD5));    }
        protected CVideoList SelectByFBId(CPagingInfo pi, long videoFBId)    {    return SelectWhere(pi, new CCriteriaList("VideoFBId", videoFBId));    }

        //Count
        protected int SelectCountByMD5(Guid videoMD5)   {   return SelectCount(new CCriteriaList("VideoMD5", videoMD5));     }
        protected int SelectCountByFBId(long videoFBId)   {   return SelectCount(new CCriteriaList("VideoFBId", videoFBId));     }

        //Transactional
        internal CVideoList SelectByMD5(Guid videoMD5, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("VideoMD5", videoMD5), tx);    }
        internal CVideoList SelectByFBId(long videoFBId, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("VideoFBId", videoFBId), tx);    }
        #endregion

        #region Static - Cache Implementation
        public static CVideoList Cache
        {
            get
            {
                CVideoList cache = (CVideoList)CCache.Get(CACHE_KEY);
                if (cache == null)
                {
                    lock (typeof(CVideoList))
                    {
                        cache = (CVideoList)CCache.Get(CACHE_KEY);
                        if (cache == null)
                        {
                            cache = LoadCache();
                            CVideo.Cache = cache;
                        }
                    }
                }
                return cache;
            }
            set
            {
                SetCache(value);   //Not locked, because cache gets cleared at anytime anyway
            }
        }

        //Change Management:
        //Clone, modify, and then replace the cache (threadsafe for anything iterating the collection)
        //Note that internal indices are dicarded each time unless handled in constructor and add/remove overrides
        protected override void CacheDelete()
        {
            if (CacheIsNull)
                return;

            lock (typeof(CVideoList))
            {
                CVideoList temp = new CVideoList(Cache);
                int size = temp.Count;
                temp.Remove(this);
                if (size == temp.Count) //Remove might fail if cache was refreshed with new instances. Use old index
                    temp.Remove(CacheGetById(temp));
                Cache = temp;
            }
        }
        protected override void CacheInsert()
        {
            if (CacheIsNull)
                return;

            lock (typeof(CVideoList))
            {
                CVideoList temp = new CVideoList(Cache);
                temp.Add(this);
                SetCache(temp);
            }
        }
        protected override void CacheUpdate()
        {
            if (CacheIsNull)
                return;

            lock (typeof(CVideoList))
            {
                CVideoList temp = new CVideoList(Cache);
                if (!temp.Contains(this))
                {
                    temp.Remove(CacheGetById(temp));
                    temp.Add(this);
                }
                SetCache(temp);
            }
        }
        protected override void CacheClear()
        {
            SetCache(null);
        }
        protected static bool CacheIsNull
        {
            get
            {
                return null == CCache.Get(CACHE_KEY);
            }
        }
        #endregion
        #region ToXml
        protected override void ToXml_Autogenerated(System.Xml.XmlWriter w)
        {
            Store(w, "VideoId", this.VideoId);
            Store(w, "VideoFileName", this.VideoFileName);
            Store(w, "VideoFileSize", this.VideoFileSize);
            Store(w, "VideoDescription", this.VideoDescription);
            Store(w, "VideoMD5", this.VideoMD5);
            Store(w, "VideoCreated", this.VideoCreated);
            Store(w, "VideoBlob", this.VideoBlob);
            Store(w, "VideoFBId", this.VideoFBId);
            Store(w, "VideoThumb", this.VideoThumb);
        }
        #endregion

    }
}
